var util = require('../../libs/util.js');
var app = getApp();
var sliderWidth = 120; // 需要设置slider的宽度，用于计算中间位置
var startX = 0, endX = 0, prevScrollLeft = 0, prevId = 0, prevIndex = 0;
var px = 0, py = 0, px2 = 0, py2 = 0, windowWidth = 0;
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    hasData: false,
    middle_height: "100vh",
    detailShow: '',
    dd_style: "",
    path_data: [],
    month_knot_text: '全部结算方式',
    month_knot: '',
    priceType_text: '应收和应付',
    priceType: '',
    warehouse: '全部仓库',
    warehouseId: '',
    is_schedule: false,
    shareMode: 0,
    tab: 1,
    stats: { nostart: 0, undertaking: 0, receiving_order: 0, delivery: 0, delivery_end: 0, end: 0 },
    coAdmin: app.globalData.coAdmin
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onReady: function (e) {
  },
  onShow: function (e) {
    var that = this;
    wx.getStorage({
      key: 'monitor_warehouse',
      success: function (res) {
        if (res.data) {
          that.setData({
            warehouse: String(res.data.abbreviation || res.data.name).substr(0, 10),
            warehouseId: res.data.value
          });
          that.loadData();
        }
      },
      complete: function (res) {
        wx.removeStorage({
          key: 'monitor_warehouse',
          success: function (res) {
          }
        });
      }
    });
  },
  onLoad: function (e) {
    var that = this;
    wx.setNavigationBarTitle({ "title": app.appName + "--运单核验" });

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          middle_height: res.windowHeight - 40,
          detail_height: res.windowHeight - 40
        });
        windowWidth = res.windowWidth;
      }
    });
    if (e.warehouseid) {
      that.setData({
        warehouse: e.warehouse,
        warehouseId: e.warehouseid
      });
    }
    if (e.shareMode) {
      that.setData({
        shareMode: 1
      })
    }
    that.loadData();
  },
  switchChange(e) {
    console.log(e)
    var that = this;
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  loadData: function () {
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    util.getData("/atapi/delivery/process/deliveryVerificationList", {
      CO_ID: app.globalData.CO_ID,
      month_knot: that.data.month_knot,
      warehouseId: that.data.warehouseId,
      priceType: that.data.priceType,
      'page': that.data.page,
      'pageSize': that.data.pageSize
    }, function (res, error) {
      console.log(res);
      console.log(error)
      wx.hideLoading();
      if (res.data.success) {
        that.setData({
          hasData: true,
          delivery_data: res.data.data,
          stats: res.data.stats
        });
      } else {
        that.setData({
          hasData: false,
          delivery_data: [],
          stats: 0
        })
      };
    });

  },
  verification: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    wx.showModal({
      title: '温馨提示',
      content: '确定已核验？确定后将进入财务处理流程',
      success(res) {
        if (res.confirm) {
          wx.showLoading({
            title: '提交中',
          })
          util.postData("/atapi/delivery/process/deliveryVerification", {
            id: id
          }, function (res, error) {
            console.log(res);
            console.log(error)
            wx.hideLoading();
            if (res.data.success) {
              wx.showToast({
                title: res.data.data,
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/finance/delivery_verification'
                    })
                  }, 3000);
                }
              });
            }else{
              wx.showToast({
                title: res.data.data,
                image: '/images/wrong.png',
                duration: 3000
              });
            }
          });
        }
      }
    })
  },
  changeMonthKnot: function (e) {
    var that = this;
    var itemList = ['全部结算方式', '月结', '现结']
    wx.showActionSheet({
      itemList: itemList,
      success(res) {
        if (res.tapIndex == 0){
          that.setData({
            month_knot_text: itemList[res.tapIndex],
            month_knot:''
          })
        }
        if (res.tapIndex == 1) {
          that.setData({
            month_knot_text: itemList[res.tapIndex],
            month_knot: '0'
          })
        }
        if (res.tapIndex == 2) {
          that.setData({
            month_knot_text: itemList[res.tapIndex],
            month_knot: '1'
          })
        }
        that.loadData();
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  bindInput: function (e) {
    var item = String(e.currentTarget.id), item_value = {};
    item_value[item] = e.detail.value;
    this.setData(item_value);
  },
  changePrice: function (e) {
    var that = this;
    var currentId = e.currentTarget.dataset.id;
    var currentPrice = e.currentTarget.dataset.price;
    this.setData({
      modalName: 'changePriceModal1',
      currentId: currentId,
      currentPrice: currentPrice
    })
  },
  confirmChangePrice: function (e) {
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '确定修改价格？价格修改后将以新价格作为财务核算价格',
      success(res) {
        if (res.confirm) {
          that.setData({
            modalName: ''
          })
          wx.showLoading({
            title: '提交中',
          })
          util.postData("/atapi/delivery/process/deliveryFinanceChangePrice", {
            id: that.data.currentId,
            price2: that.data.price2
          }, function (res, error) {
            console.log(res);
            console.log(error)
            wx.hideLoading();
            if (res.data.success) {
              wx.showToast({
                title: res.data.data,
                icon: 'success',
                success: function () {
                  setTimeout(function () {
                    wx.redirectTo({
                      url: '/pages/finance/delivery_verification'
                    })
                  }, 3000);
                }
              });
            } else {
              wx.showToast({
                title: res.data.data,
                image: '/images/wrong.png',
                duration: 3000
              });
            }
          });
        }
      }
    })
  },
  changePriceType: function (e) {
    var that = this;
    var itemList = ['应收和应付', '应收', '应付']
    wx.showActionSheet({
      itemList: itemList,
      success(res) {
        if (res.tapIndex == 0) {
          that.setData({
            priceType_text: itemList[res.tapIndex],
            priceType: ''
          })
        }
        if (res.tapIndex == 1) {
          that.setData({
            priceType_text: itemList[res.tapIndex],
            priceType: 'income'
          })
        }
        if (res.tapIndex == 2) {
          that.setData({
            priceType_text: itemList[res.tapIndex],
            priceType: 'pay'
          })
        }
        that.loadData();
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  changeWarehouse: function (e) {
    var that = this;
    wx.navigateTo({
      url: '/pages/schedule/monitor_warehouse'
    })
  },
  showDeliveryDetail: function (e) {
    var that = this;
    var id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/order/order?id=' + id
    })
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  }
})
